package com.lry.web.fiction.controller;

import com.lry.web.fiction.entity.FictionVariable;
import com.lry.web.fiction.service.FictionVariableService;
import com.lry.web.util.Result;
import com.lry.web.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/fictionVariable")
public class FictionVariableController {
    @Autowired
    FictionVariableService fictionVariableService;



    @RequestMapping(value = "/insert", method=RequestMethod.POST)
    @ResponseBody
    public Result insertFictionVariable(@ModelAttribute FictionVariable fictionVariable)  {
        int i = 0;
        if(fictionVariable.getId()!=null){
            try {
                i = fictionVariableService.updateFictionVariable(fictionVariable);
            }catch (Exception e){}
        }else{
            fictionVariable.setCreationTime(new Date());

            try {
                i = fictionVariableService.setFictionVariable(fictionVariable);
            }catch (Exception e){}
        }
        if(i!=0){
            return ResultUtil.success();
        }else{
            return ResultUtil.error(1,"保存失败");
        }
    }
    @RequestMapping("/deleteById/{id}")
    @ResponseBody
    public Result deleteFictionVariable(@PathVariable(value = "id") String id)  {
        List<String> listFVId = Arrays.asList(id.split(","));
        int i = 0;
        for(String idd: listFVId){
             i = fictionVariableService.delete(Integer.valueOf(idd));
        }
        if(i!=0){
            return ResultUtil.success();
        }else{
            return ResultUtil.error(1,"删除失败");
        }
    }

    @RequestMapping(value="/verifyFictionVariable/{name}", method=RequestMethod.POST)
    @ResponseBody
    public Result verifyFictionVariable(@ModelAttribute FictionVariable fictionVariable,@PathVariable(value = "name") String name){
        Map<String,Object> map = null;
        try {
            map =  fictionVariableService.verifyFictionVariable(fictionVariable,name);
        }catch (Exception e){
            return ResultUtil.error(1,"验证失败,出现错误");
        }
        return ResultUtil.success(map);

    }
}
